<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\index\controller;
use app\index\controller\Base;
use think\Controller;
use think\Db;
use think\Session;
use think\Cache;
use aldy\SmsDemo;
class Teacher extends  Base
{
    public function dianping(){
        $post=input('post.');
        $data = [
            'dianping'=>$post['dianping'],
            'is_dp'=>1,
            'dptime'=>time()
        ];
        $res = db('daka')->where(['acid'=>1,'id'=>$post['id'],'tid'=>$post['tid']])->update($data);
        if($res){
            $this->result(0,'点评成功','',0,'json');
        }
    }
    public function dianpinglist()
    {
        $post=input('post.');
        $map=[];
        $map['acid']=1;
        $day = $post['day']!=''?strtotime($post['day']):$post['day'];
        if($post['courseid']!=0){
            $map['menuid']=$post['courseid'];
        }
        if($post['tid']!=0){
            $map['tid']=$post['tid'];
        }
        if($post['type']=='0'){
            $course_db='coursemenu';
        }else{
            $course_db='pxcourse';
        }
        if($post['day']!=''){
            $data = db('daka')
                ->where($map)
                ->where('addtime','gt',$day)
                ->where('addtime','lt',$day+3600*24)
                ->where(['type'=>$post['type']])
                ->select();
        }else{
            $data = db('daka')
                ->where($map)
                ->where(['type'=>$post['type']])
                ->select();
        }

        foreach ($data as $k=>$v){
            $userinfo=db('user')->where(['acid'=>1,'id'=>$v['uid']])->find();
            $courseinfo=db($course_db)->where(['acid'=>1,'id'=>$v['menuid']])->find();
            $data[$k]['nickname'] = $userinfo['nickname'];
            $data[$k]['avatar'] = $userinfo['avatar'];
            $data[$k]['realname'] = $userinfo['realname'];
            $data[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            if($v['dptime'] && $v['dptime']!='' && $v['dptime']!='null'){
                $data[$k]['dptime'] = date('Y-m-d H:i:s',$v['dptime']);
            }
            $data[$k]['coursename'] = $courseinfo['menuname']??$courseinfo['name'];
            $data[$k]['daytotime'] =$day;
        }
        if(!empty($data)){
            $this->result(0,'获取作业点评数据成功',$data,0,'json');
        }else{
            $this->result(1,'暂无作业点评数据','',0,'json');
        }
    }

    public function test(){
        $post=input('post.');
        $userinfo=db('user')->where(['acid'=>1,'id'=>$post['uid']])->find();
        $data=array(
            'acid'=>1,
            'imgname'=>$userinfo['nickname'],
            'img'=>$userinfo['avatar'],
            'tphone'=>$userinfo['phone'],
            'addtime'=>time(),
            'uid'=>$post['uid'],
            'is_verify'=>0,
            'is_test'=>1
        );
        $res=db('teacher')->insert($data);
        if($res){
            $this->result(0,'新增测试讲师成功','',0,'json');
        }
    }
    public function course_edit(){
        $post=input('post.');
        $id=$post['id'];
        unset($post['id']);
        $res=db('coursemenu')->where(['acid'=>1,'id'=>$id])->update($post);
        if($res){
            $this->result(0,'修改成功','',0,'json');
        }
    }
    public function mycourse(){
        $tid=input('post.tid');
        $courses=db('coursemenu')->where(['acid'=>1,'tid'=>$tid])->select();
        if(!empty($courses)){
            foreach ($courses as $k=>$v){
                $ksnum=db('course')->where(['acid'=>1,'menuid'=>$v['id']])->count();
                $courses[$k]['ksnum']=$ksnum;
                $courses[$k]['thumb']= to_media_url($courses[$k]['thumb']);
            }
            $this->result(0,'获取课程数据成功',$courses,0,'json');
        }else{
            $this->result(1,'暂无课程数据','',0,'json');
        }
    }
    public function tinfo(){
        $uid=input('post.uid');
        $tinfo=db('teacher')->where(['acid'=>1,'uid'=>$uid])->find();
        if(!empty($tinfo)){
            $tinfo['img']=to_media_url($tinfo['img']);
            $this->result(0,'获取讲师详情数据成功',$tinfo,0,'json');
        }else{
            $this->result(1,'讲师未注册','',0,'json');
        }
    }
    public function index(){
        $map=array(
            'acid'=>1,
        );
        $teachers=Db::name('teacher')
            ->where($map)
            ->where(['is_verify'=>1,'is_test'=>0])
            ->select();
        if(!empty($teachers)) {
            foreach ($teachers as &$v) {
                $v['img'] = to_media_url($v['img']);
            }
        }
        $data=array(
            'teachers'=>$teachers,
        );
        $this->result(0,'获取讲师列表数据成功',$data,0,'json');
    }
    //   讲师详情
    public function detail(){
        $tid=input('post.tid');
        $map=array(
            'id'=>$tid,
            'acid'=>1,
        );
        $teacher=Db::name('teacher')
            ->where($map)
            ->find();
        $courses=db('coursemenu')->where(['acid'=>1,'tid'=>$tid])->select();
        if(!empty($courses)){
            foreach ($courses as $k=>$v){
                $ksnum=db('course')->where(['acid'=>1,'menuid'=>$v['id']])->count();
                $courses[$k]['ksnum']=$ksnum;
                $courses[$k]['thumb']= to_media_url($courses[$k]['thumb']);
            }
        }
        $data=array(
            'teacher'=>$teacher,
            'courses'=>$courses
        );
        $this->result(0,'获取讲师详情数据成功',$data,0,'json');
    }
    public function sendcode(){
        $Sms= new SmsDemo();
        $phoneNum=input('post.phone');
        $code = rand(100000,999999);
        $signName="知识付费在线课程";
        $TemplateCode=config('alidy.TemplateCode');
        $data = $Sms->sendSms($phoneNum,$signName,$TemplateCode,$code);
        if($data){
            $options = [
                // 缓存类型为File
                'type'   => 'File',
                // 缓存有效期为永久有效
                'expire' => 3600,
                // 指定缓存目录
                'path'   => APP_PATH . 'runtime/cache/',
            ];
            cache($options);
            cache('code', $code);
            cache('phone', $phoneNum);
            cache('codetime', time());
            $this->result(0,'发送成功',$data,0,'json');
        }
    }
    public function js_reg(){
        $map=array(
            'acid'=>1
        );
        $post=input('post.');
        $phone=$post['phone'];
        if($phone!=''){
            if (time()-cache('codetime')>300) {
                cache('code', NULL);
                $this->result(3,'验证码超时失效！',array('codetime'=>cache('codetime')),0,'json');
            }else{
                if (cache('code')!=$post['code']) {
                    $this->result(1,'验证码不正确！','',0,'json');
                }elseif (cache('phone')!=$post['phone']){
                    $this->result(1,'提交手机号非验证手机号,请重试！','',0,'json');
                }else{
                    $phone_reg=Db::name('teacher')
                        ->where($map)
                        ->where('tphone',$phone)
                        ->find();
                    $data=array(
                        'uid'=>$post['uid'],
                        'imgname'=>$post['name'],
                        'img'=>$post['img'],
                        'tphone'=>$post['phone'],
                        'tpassword'=>md5(substr($post['phone'],-6)),
                        'is_verify'=>0,
                        'addtime'=>time()
                    );
                    if(!empty($phone_reg)){
                        $this->result(1,'手机号已被占用！','',0,'json');
                    }else{
                        $data['acid']=1;
                        $res=Db::name('teacher')->insert($data);
                        if($res){
                            $this->result(0,'请等待审核！','',0,'json');
                        }
                    }
                }
            }
        }
    }
}
